package com.uniknow.app.ui.fragment;

import roboguice.inject.InjectView;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.uniknow.app.R;
import com.uniknow.app.util.Util;
import com.uniknow.app.widget.FootBtnView;
import com.uniknow.app.widget.FootBtnView.OnFootBtnClickListener;

public class Frag_Main extends BaseFragment{
	private static final int PAGE_COUNT = 6;
	
	public static final int PAGE_HOME = 1,
							PAGE_SOLUTION  = 2,
							PAGE_RECORD  = 3, 
							PAGE_MARKET  = 4,
							PAGE_USER_CENTRE  = 5;
	
	private String[] pageTitles;
	private int img[] = {
			R.drawable.foot_1,	R.drawable.foot_2,R.drawable.foot_5,R.drawable.foot_3,	R.drawable.foot_4,//获取焦点后状态
			R.drawable.foot_11,	R.drawable.foot_22,R.drawable.foot_55,R.drawable.foot_33,R.drawable.foot_44,//正常状态
			};
	
	private int currPageIndex = PAGE_HOME;	
	
	private int animationTwords = 0;
	
	private String titleStr ;
	
	private BaseFragment[] fragPages;
	private BaseFragment currentFragment;
	
	@InjectView(R.id.footBtnView1)
	private FootBtnView footBtnView;
	
	@InjectView(R.id.main_splite_line)
	private View spliteLineView;
	
	@InjectView(R.id.main_splite_shadow)
	private View shadowView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);	
		initData();
	}
	
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				
				break;

			default:
				break;
			}
		}
		
	};
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.frag_main, null);
		return view;
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		
		footBtnView = (FootBtnView) view.findViewById(R.id.footBtnView1);		
		footBtnView.init(img,pageTitles);		
		footBtnView.setOnFootBtnClickListener(new OnFootBtnClickListener() {
			
			@Override
			public void onClick(View v, int pos) {	
				selectItem(pos);
			}
		});
		
		selectItem(0);
		setTitleLeftVisiable(false);
		setTitleRightButton(false, null);
		
		initBottomTab();
	}
	
	private void initBottomTab(){
		//魅族手机的smartBar 适配
		if(Util.hasSmartBar()){
			footBtnView.setVisibility(View.GONE);
			spliteLineView.setVisibility(View.GONE);
			shadowView.setVisibility(View.GONE);
			setHasOptionsMenu(true);
		}else{
			footBtnView.setVisibility(View.VISIBLE);
			spliteLineView.setVisibility(View.VISIBLE);
			shadowView.setVisibility(View.VISIBLE);
		}
	}
	

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		if(Util.hasSmartBar()){
			inflater.inflate(R.menu.frag_main_menu, menu);
		}
		
	}
	
	@SuppressLint("NewApi")
	public void selectTabOnOther(int index){
		if(Util.hasSmartBar()){
			selectItem(index);
			getActivity().invalidateOptionsMenu();
		}else{
			if(index >= 3){
				index +=1;
			}
			footBtnView.callOnclick(index);
		}
	}

	@SuppressLint("NewApi")
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		 switch (item.getItemId()) {
         case R.id.tab_main_home:
        	 selectItem(0);
             break;
         case R.id.tab_main_service:
        	 selectItem(1);
             break;
         case R.id.tab_main_record:
        	 selectItem(2);
             break;
         case R.id.tab_main_my:
        	 selectItem(4);
             break;
         default:
             break;
         }		
		getActivity().invalidateOptionsMenu();
		return super.onOptionsItemSelected(item);
	}
	
	
	
	 /**
	 * 方法名：refreshTabIcon
	 * 描述： 更改smartBar中按钮图标的选中状态---（魅族手机）
	 * @参数：   参数名  参数类型   参数描述
	 * @返回值类型： void   
	 */
	private void refreshTabIcon(MenuItem item) {
		 
		 switch (item.getItemId()) {
         case R.id.tab_main_home:
        	 item.setIcon(currPageIndex == PAGE_HOME ? R.drawable.foot_11
                     : R.drawable.foot_1);
             break;
         case R.id.tab_main_service:
        	 item.setIcon(currPageIndex == PAGE_SOLUTION ? R.drawable.foot_22
                     : R.drawable.foot_2);
             break;
         case R.id.tab_main_record:
        	 item.setIcon(currPageIndex == PAGE_RECORD ? R.drawable.foot_33
                     : R.drawable.foot_3);
             break;
         case R.id.tab_main_my:
        	 item.setIcon(currPageIndex == PAGE_USER_CENTRE ? R.drawable.foot_44
                     : R.drawable.foot_4);
             break;
         default:
             break;
         }		
		
	}
	 
	@Override
	public void onPrepareOptionsMenu(Menu menu) {
		for (int i = 0; i < menu.size(); i++) {
            refreshTabIcon(menu.getItem(i));
		}
		super.onPrepareOptionsMenu(menu);
	}
		
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mHandler.sendEmptyMessageDelayed(0, 100);
	}
	
	@Override
	public void refreshView() {
		// TODO Auto-generated method stub		
		reSetTitle();
		if(currentFragment != null){
			currentFragment.refreshView();
		}
	}
	@Override
	public void onHiddenChanged(boolean flag) {
		// TODO Auto-generated method stub
		super.onHiddenChanged(flag);
		if(currentFragment != null){
			currentFragment.onHiddenChanged(flag);
		}
	}
	@Override
	public boolean onTitleRightClick(View v) {
		if(currentFragment != null){
			return currentFragment.onTitleRightClick(v);
		}else{
			return super.onTitleRightClick(v);	
		}
		
	}
	
	private void initData(){
		pageTitles = getResources().getStringArray(R.array.main_tab_items);
	}
	
	private void selectItem(int position) {	
		int temp = currPageIndex;
		
		switch(position){		
		case 0:
			currPageIndex = PAGE_HOME;
			break;
		case 1:
			currPageIndex = PAGE_SOLUTION;
			break;
		case 2:
			currPageIndex = PAGE_RECORD;
			break;
		case 3:
			currPageIndex = PAGE_MARKET;
			break;
		case 4:
			currPageIndex = PAGE_USER_CENTRE;
			break;		
		}
		
		//动画方向判断
		if(currPageIndex < temp){
			animationTwords = -1;
		}else if(currPageIndex > temp){
			animationTwords = 1;
		}else{
			animationTwords = 0;
		}
		
		titleStr =pageTitles[position];
		setTitile(titleStr);
		changeCurrentPage(getPageFrag(currPageIndex));
	
	}
	
	public void reSetTitle(){
		setTitleLeftVisiable(false);
		setTitleRightButton(false, null);
		if(titleStr != null ){
			setTitile(titleStr);	
		}
		
	}
	
	/** 
	 * 方法名：getPageFrag
	 * 描述：
	 * @参数：   参数名  参数类型   参数描述
	 * @返回值类型： BaseFragment   
	 * @创建时间：  2014年6月20日 
	 * @创建者：zhangyao
	 * @变更记录：2014年6月20日下午2:57:11 by
	 */
	public BaseFragment getPageFrag(int index) {
		BaseFragment frag = null ;
		
		if(fragPages == null) {
			fragPages = new BaseFragment[PAGE_COUNT];	
		}
		
		switch (index) {
		case PAGE_HOME:
			if(fragPages[0] == null) {
				fragPages[0] = new Frag_Home();
			}
			frag = fragPages[0];		
			break;	
		/*case PAGE_SOLUTION:
			if(fragPages[1] == null) {
				fragPages[1] = new Frag_Solution();
			}
			frag = fragPages[1];		
			break;	
		case PAGE_RECORD:
			if(fragPages[2] == null) {
				fragPages[2] = new Frag_Records();
			}
			frag = fragPages[2];	
			break;	
		case PAGE_MARKET:
			if(fragPages[3] == null) {
				fragPages[3] = new Frag_Market();
			}
			frag = fragPages[3];		
			break;	
		case PAGE_USER_CENTRE:
			if(fragPages[4] == null) {
				fragPages[4] = new Frag_UserCentre();
			}
			frag = fragPages[4];		
			break;	
		default:
			if(fragPages[0] == null) {
				fragPages[0] = new Frag_HomePage();
			}
			frag = fragPages[0];
			break;*/
		}
		
		return frag;
	}
	
	/** 
	 * 方法名：changeCurrentPage
	 * 描述：
	 * @参数：   参数名  参数类型   参数描述
	 * @返回值类型： void   
	 * @创建时间：  2014年6月20日 
	 * @创建者：zhangyao
	 * @变更记录：2014年6月20日下午2:25:54 by
	 */
	public void changeCurrentPage(BaseFragment frag) {
		
		if(currentFragment == frag) {			
			return ;
		}
		
		currentFragment = frag;		
		FragmentManager fragmentManager = getChildFragmentManager();		
		FragmentTransaction  transation = fragmentManager.beginTransaction();

		if(animationTwords < 0){
			transation.setCustomAnimations(R.anim.leftin,  R.anim.rightout);
		}else if(animationTwords > 0){
			transation.setCustomAnimations(R.anim.rightin,  R.anim.leftout);
		}		
		
		transation.replace(R.id.main_tab_page, currentFragment).commit();
		
	}

	public BaseFragment getCurrentFragment() {
		return currentFragment;
	}

	

}
